// api.js
import axios from "axios";
import { ElMessage } from "element-plus";
import { identity } from "../stores/identity";
const api = axios.create({
  baseURL: `http://www.cetastar.com/api/`, // 你的API基础URL
  timeout: 10000, // 请求超时时间
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
});
const config = {
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    Authorization: identity.authorization, // 使用 Bearer Token 示例，如果是其他类型的 token，请根据实际情况调整
  },
};
const open = (msg: any) => {
  ElMessage(msg);
};
// 封装GET请求
function get(url: string, params = {}) {
  config.headers.Authorization = identity.authorization;
  return api
    .get(url, { ...params, ...config })
    .then((response) => response.data)
    .catch((error) => {
      open("错误1：" + error);
    });
}

// 封装POST请求
function post(url: string, data = {}, params = {}) {
  let newUrl = url;
  if (Object.keys(params).length) {
    newUrl = newUrl + "?";
    for (const [key, value] of Object.entries(params)) {
      newUrl = newUrl + `${key}=${value}&`;
    }
    newUrl = newUrl.slice(0, -1);
  }
  config.headers.Authorization = identity.authorization;
  return api
    .post(newUrl, data, config)
    .then((response) => response.data)
    .catch((error) => {
      open("错误1：" + error);
    });
}
export class requestAxios {
  static get = get;
  static post = post;
}
// 可以根据需要添加PUT, DELETE等方法
